<% 
@vm.firewalls(eager: [:firewall_rules, :location])
viewable_fws =
  dataset_authorize(Firewall.where(id: @vm.firewalls.map(&:id)), "Firewall:view")
    .select_hash(:id, Sequel.as(true, :v))
    .transform_keys! { UBID.from_uuidish(it).to_s } %>

<div id="vm-firewall-rules" class="p-6">
  <%== part(
    "components/table_card",
    title: "Applied Firewall Rules",
    headers: ["Firewall", "CIDR", "Port Range"],
    empty_state: "This VM doesn't have applied firewall",
    rows:
      @vm.firewalls.flat_map do |fw|
        view_perm = viewable_fws[fw.ubid]
        fw.firewall_rules.map do |fwr|
          [
            [[fw.name, view_perm ? { link: path(fw)} : {}], fwr.cidr, fwr.display_port_range],
            { id: "firewall-rule-#{fwr.ubid}" }
          ]
        end
      end
  ) %>
</div>
